<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Material;
use App\Models\Project;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;

class MaterialController extends AdminController
{
    public bool $create = false;
    public bool $edit = false;
    public string $delete_permission_name = 'material-delete';
    public bool $disable_row_selector = false;

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new Material(), function (Grid $grid) {
            $grid->model()->with('project')->orderByDesc('id');
            $grid->column('id');
            $grid->column('project.title', trans('material.fields.project_id'));
            $grid->column('name');
            $grid->column('size')->display(function ($value) {
                return getRealSize($value);
            });
            $grid->column('ext');
            $grid->column('buy')->bool();
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('project_id')->select(Project::query()->pluck('title', 'id'));
                $filter->like('name');

            });
            //
            //$grid->showBatchDelete();
            $this->gridCommon($grid);

        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new Material(), function (Form $form) {
            $form->display('id');
            $form->text('project_id');
            $form->text('name');
            $form->text('path');
            $form->text('size');
            $form->text('ext');
            $form->text('buy');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
